import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/cupertino.dart' hide Action;
import 'package:toast/toast.dart';
import 'package:www.znpigai.teacher/global_store/state.dart';
import 'package:www.znpigai.teacher/utils/http.dart';
import 'package:www.znpigai.teacher/utils/nav.dart';
import 'package:www.znpigai.teacher/utils/progress_dialog.dart';
import 'package:www.znpigai.teacher/utils/util.dart';

import 'action.dart';
import 'state.dart';

Effect<DestroyState> buildEffect() {
  return combineEffects(<Object, Effect<DestroyState>>{
    DestroyAction.onDelete: _onDelete,
  });
}

void _onDelete(Action action, Context<DestroyState> ctx) async {
  Util.showConfirmDialog('注销账号', '账号删除后不可恢复，请谨慎操作', ctx.context)
      .then((ok) async {
    if (ok == true) {
      var pr = new ProgressDialog(ctx.context)..setMessage('请等待...');
      pr.show();
      var url = '/correct/join/api/remove.mvc';
      final query = {'identity': GlobalState.isTeacher ? 'TEACHER' : 'STUDENT'};
      var result = await Http.get(url, query: query);
      pr.hide();
      if (result.success) {
        GlobalState.isSignIn = false;
        Navigator.of(ctx.context).pushNamedAndRemoveUntil(
            GlobalState.isTeacher ? Nav.SOCIAL_LOGIN : Nav.LOGIN,
            (Route<dynamic> route) => false);
      } else {
        Toast.show(result.errorMsg, ctx.context);
      }
    }
  });
}
